/**
 * DownloadTableModel.java
 * @author: liuex
 * 2007-12-29 ����10:16:39
 *
 * note: 
 */
package lex.mtd.ui.details;

import javax.swing.table.AbstractTableModel;

import org.lex.utils.text.SizeFormat;
import org.lex.utils.text.TimeFormat;

import lex.mtd.core.DownloadThread;
import lex.mtd.core.DownloadThreadSet;

public class ThreadTableModel extends AbstractTableModel
{
	private static final String[] COLUMN_NAMES = { "File", "Offset", "Length", "Transferred",
			"Progress", "Speed", "Elapsed", "Remains" };
	private DownloadThreadSet data = null;

	public ThreadTableModel()
	{
		this.data = new DownloadThreadSet( null, null );
	}

	public void setThreadSet( DownloadThreadSet threadSet )
	{
		this.data = threadSet;
		this.fireTableDataChanged();
	}

	public Object getValueAt( int row, int column )
	{
		DownloadThread part = data.getThread( row );
		switch( column )
		{
		case 0:
			// File
			return part.getFileBlock().getFileName();
		case 1:
			// Position
			return part.getFileBlock().getStartPosition();
		case 2:
			// Length
			return SizeFormat.autoFormat( part.getFileBlock().getLength() );
		case 3:
			// Transferred
			return SizeFormat.autoFormat( part.getTransferredBytes() );
		case 4:
			// Percent
			long len = part.getFileBlock().getLength();
			if( 0L == len )
				return 1d;
			return part.getTransferredBytes() / (double) len;
		case 5:
			// Speed
			return SizeFormat.autoFormat( part.getAverageSpeed() );
		case 6:
			// Elapsed
			return TimeFormat.format( part.getElapsedTime() );
		case 7:
			// Remains
			long remainsBytes = part.getFileBlock().getLength() - part.getTransferredBytes();
			long speed = part.getAverageSpeed();
			if( 0L == speed )
				return -1;
			return TimeFormat.format( remainsBytes * 1000L / part.getAverageSpeed() );
		}
		return "";
	}

	@Override
	public String getColumnName( int column )
	{
		return COLUMN_NAMES[column];
	}

	@Override
	public int getColumnCount()
	{
		return COLUMN_NAMES.length;
	}

	@Override
	public int getRowCount()
	{
		return data.getThreadCount();
	}
}
